<?php
namespace app\api\controller\user;
use app\admin\model\user\User;
use app\api\validate\user\RegisterValidate;
use app\common\annotation\ApiNodeAnnotation;
use app\common\controller\ApiBase;
use app\api\validate\user\LoginValidate;
use think\Request;

/**
 * @ApiNodeAnnotation(
 *     title="注册板块",
 *     parent="api/user.index",
 *     is_release=0,
 *     auth=1,
 * )
 */
class Register extends ApiBase
{
    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new User();
    }

    /**
     * @ApiNodeAnnotation(
     *     title="用户注册",
     *     parent="api/user.register",
     *     is_release=1,
     *     auth=1,
     * )
     */
    public function index(Request $request)
    {
        //系统设置
        $setting = get_option("system_setting");
        //请求限定
        if($request->isAjax()||$request->isPost())
        {
            //前台提交数据
            $input = $request->post();
            //验证项
            $valiDate = new RegisterValidate();
            //验证空数据
            if(!$valiDate->check($input)) $this->error($valiDate->getError());

            //手机号,电子邮件
            if(valiDatePhone($input['username']) || valiDateEmail($input['username']))
            {
                //TODO::
            }else{
                $this->error("账户无效");
            }

            //检测用户
            $user = $this->model->whereOr("username",$input['username'])
                ->whereOr("phone",$input['username'])
                ->whereOr("email",$input['username'])
                ->find();
            if($user) $this->error("该用户已存在");

            //检测系统是否设置注册验证
            if(isset($setting['open_registration']) && $setting['open_registration']==1)
            {
                //判断验证码
                if($input['smsCode']!=session("smsCode")) $this->error("验证码错误");
            }

            //创建用户
            $result = $this->model->create($input);
            //执行动作结果
            if($result)
            {
                $this->success("注册成功",'',$result);
            }else{
                $this->error("注册失败");
            }
        }
    }
}